package com.v.im.websocketservice.config;

import com.v.im.commonmodule.utils.bean.CurrentUserInfo;
import com.v.im.commonmodule.utils.utils.UserHeaderUtil;
import com.v.im.websocketservice.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

@Slf4j
@Component
public class WebSocketConfig extends ServerEndpointConfig.Configurator {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }


    private static final String HttpSession = null;

    /* 修改握手,就是在握手协议建立之前修改其中携带的内容 */
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        /*如果没有监听器,那么这里获取到的HttpSession是null*/
        Object userInfo = request.getHeaders().get(UserHeaderUtil.CURRENT_USER_INFO);
        if (userInfo != null) {
            sec.getUserProperties().put(Constants.USER_INFO, userInfo.toString());
        }else{
            log.error("获取 header 用户信息失败！");
        }
        super.modifyHandshake(sec, request, response);
    }

}
